<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="home" font-size="30px" /><span
              >江苏传智播客教育科技股份有限公司</span
            >
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')"
                      >添加子部门</el-dropdown-item
                    >
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 头部的布局结束： -->
        <!-- 主体内容的布局开始： -->
        <el-tree :data="list">
          <!-- 使用插件在el-tree自定义内容 -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="home" font-size="30px" /><span>{{
                  data.name
                }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)"
                          >添加子部门</el-dropdown-item
                        >
                        <el-dropdown-item @click.native="hEdit(data.id)"
                          >编辑部门</el-dropdown-item
                        >
                        <el-dropdown-item
                          v-if="data.children && data.children.length === 0"
                          @click.native="hDel(data.id)"
                          >删除子部门</el-dropdown-item
                        >
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <!-- 准备弹层 -->
      <el-dialog
        :title="isEdit ? '编辑部门' : '新增子部门'"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @close="hClose"
      >
        <!-- 新增或者编辑子部门的组件 -->
        <addOrEdit
          :id="curId"
          ref="myDept"
          :old-list="oldList"
          :is-edit="isEdit"
          @close="showDialog = false"
          @success="hSuccess"
        />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getDepartment, delDepartment } from '@/api/departments'
import { transTreeList } from '@/utils/index'
import addOrEdit from '@/views/departments/deptDialog.vue'
export default {
  components: {
    addOrEdit
  },
  data() {
    return {
      // 依赖一份树形数据
      oldList: [],
      list: [],
      showDialog: false,
      curId: '', // 父级的id
      isEdit: false
    }
  },
  created() {
    this.loadgetDepartment()
  },
  methods: {
    hSuccess() {
      // 关闭弹层
      this.showDialog = false
      // 重新刷新页面
      this.loadgetDepartment()
    },
    // 获取部门的列表
    async loadgetDepartment() {
      try {
        const res = await getDepartment()
        console.log(res.data.data.depts, 'res.data.data.depts')
        this.oldList = res.data.data.depts.map((item) => {
          const { name, code, pid, id } = item
          return { name, code, pid, id }
        })
        // 进行数据的绑定
        this.list = transTreeList(res.data.data.depts)
      } catch (error) {
        console.log(error)
      }
    },
    // 删除部门
    hDel(id) {
      this.$confirm('您确认删除该部门吗, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const res = await delDepartment(id)
          console.log(res, 'delDepartment')
          this.$message.success('删除成功！')
          if (res.success) {
            this.loadgetDepartment()
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    hClose() {
      this.$refs.myDept.resetForm()
    },
    hEdit(id) {
      this.curId = id
      this.isEdit = true
      this.showDialog = true
      // 解决数据不更新的方式2,在父组件中调用子组件的方法
      this.$nextTick(() => {
        this.$refs.myDept.loadgetDepartmentDetail()
      })
    },
    hAdd(id) {
      this.curId = id
      this.isEdit = false
      this.showDialog = true
    }
  }
}
</script>
